// 解析列表数据 -> json 数组
export function getListDataDecode(data) {
  const dataLength = 192
  let originalData = data
  const listDataSource = []
  // const version = decodeIntData(originalData.slice(0, 4)) // 前4个字节表示文件版本
  const count = decodeIntData(originalData.slice(4, 8)) // 第二个4字节表示文件内容数量
  originalData = data.slice(8, dataLength * count + 8)
  for (let i = 0; i < count; i++) {
    const currentModelData = originalData.slice(0, dataLength)
    listDataSource.push(analyseData(currentModelData))
    originalData = originalData.slice(dataLength, originalData.length)
  }
  return listDataSource
}

// 获取到收益率
export function getMatrixDataDecode(data, name) {
  const result = {
    name: '',
    rate: [],
    value: [],
    oneMonthValue: 0,
    threeMonthValue: 0,
    sixMonthValue: 0,
    oneYearValue: 0
  }
  const dataLength = 28
  const originalData = data
  const count = originalData.length / dataLength
  const valueResult = []
  const rateResult = []
  let position = 0
  let firstValue = 0
  // 遍历全部字符
  for (let i = 0; i < count; i++) {
    // 取出一个子数据
    const subData = originalData.slice(
      i * dataLength,
      i * dataLength + dataLength
    )
    // 取出时间戳
    const time = subData.slice(0, 4)
    const timeNumber = decodeTimeNumber(time)
    // 取出的净值
    const valueBuffer = subData.slice(20, 28)
    const value = valueBuffer.readDoubleLE(0)
    // 组装成净值模型
    const valueModel = {
      time: timeNumber,
      value: value
    }
    if (i === 0) {
      firstValue = value
    }
    // 组装成收益率模型
    const rateModel = {
      time: decodeTimeString(time),
      rate: (value - firstValue) / firstValue
    }
    valueResult.push(valueModel)
    rateResult.push(rateModel)
    // 改变位置
    position = position + dataLength
  }
  result.name = name
  result.rate = rateResult
  result.value = valueResult
  result.oneMonthValue = getValue(1, valueResult)
  result.threeMonthValue = getValue(3, valueResult)
  result.sixMonthValue = getValue(6, valueResult)
  result.oneYearValue = getValue(12, valueResult)
  return result
}

export function get399300LineData(data) {
  const dataLength = 40
  const count = data.length / dataLength
  // const dateArray = new Array()
  const priceArray = []
  for (let i = 0; i < count; i++) {
    const subData = data.slice(i * dataLength, i * dataLength + dataLength)
    const date = decodeIntData(subData.slice(0, 4)).toString()
    const dateString =
      date.slice(0, 4) + '-' + date.slice(4, 6) + '-' + date.slice(6, 8)
    const closeprice = decodeIntData(subData.slice(28, 32))
    priceArray[dateString] = closeprice
  }
  return priceArray
}

// 解析 -> 数据json对象
function analyseData(data) {
  const model = {
    enName: '',
    cnName: '',
    klineType: 0,
    paramName: '',
    createdTiem: 0,
    beginTime: 0,
    endTime: 0,
    selected: '',
    running: '',
    factorsGroupLen: 0,
    author: '',
    oneMonthRate: '100%',
    threeMonthRate: '100%',
    sixMonthRate: '100%',
    charData: []
  }
  model.enName = decodeStringData(data.slice(0, 50))
  model.cnName = decodeStringData(data.slice(50, 100))
  model.klineType = decodeIntData(data.slice(100, 104))
  model.paramName = decodeStringData(data.slice(104, 154))
  model.createdTiem = decodeTimeString(data.slice(154, 158))
  model.beginTime = decodeTimeString(data.slice(158, 162))
  model.endTime = decodeTimeString(data.slice(162, 166))
  model.selected = decodeStringData(data.slice(166, 167))
  model.running = decodeStringData(data.slice(167, 168))
  model.factorsGroupLen = decodeIntData(data.slice(168, 172))
  model.author = decodeStringData(data.slice(172, 192))
  return model
}

// 获取指定月数的收益
function getValue(monthNumber, dataArray) {
  const valueResult = {
    rate: 0.1,
    index: 0
  }
  const timeInterval = 2592000 * 1000 * monthNumber
  const lastValue = dataArray[dataArray.length - 1]
  const aimTime = lastValue.time - timeInterval
  let aimValue
  let aimIndex
  // 寻找目标点数据
  for (let i = dataArray.length - 1; i >= 0; i--) {
    const subdata = dataArray[i]
    if (subdata.time < aimTime) {
      aimValue = subdata.value
      aimIndex = i
      break
    }
  }
  const rate = (lastValue.value - aimValue) / lastValue.value
  valueResult.rate = rate
  valueResult.index = aimIndex
  return valueResult
}

// 解码字符串
function decodeStringData(data) {
  // 该处遍历是为了找到为0的字节，他在字符串中不显示，但拼接成URL时会表现为%00导致请求错误
  let indexToCut = data.length - 1
  for (let i = 0; i < data.length; i++) {
    if (data[i] === 0) {
      indexToCut = i
      break
    }
  }
  const subdata = data.slice(0, indexToCut)
  const string = subdata.toString('UTF8')
  return string
}

// 解码int数字
function decodeIntData(data) {
  const intNumber = data.readUInt32LE(0)
  return intNumber
}

// 解码时间戳为字符串
function decodeTimeString(data) {
  const timeInt = decodeIntData(data) * 1000
  const date = new Date(new Date(timeInt))
  const year = date.getFullYear()
  let month = date.getMonth() + 1
  if (month < 10) {
    month = '0' + month
  }
  let day = date.getDate()
  if (day < 10) {
    day = '0' + day
  }
  // const hour = date.getHours()
  // const minute = date.getMinutes()
  const dateString = year + '-' + month + '-' + day
  return dateString
}

// 解码时间戳为数字
function decodeTimeNumber(data) {
  const timeInt = decodeIntData(data) * 1000
  return timeInt
}
